package com.yunduansing.demo.web.system;

import java.util.List;

import com.yunduansing.demo.entity.PostEntity;
import com.yunduansing.demo.exception.RecordExistsException;
import com.yunduansing.demo.model.common.PageRequestModel;
import com.yunduansing.demo.model.common.PageResultModel;
import com.yunduansing.demo.model.common.ResponseModel;
import com.yunduansing.demo.model.post.PostTreeModel;
import com.yunduansing.demo.service.PostService;
import com.yunduansing.demo.web.BaseController;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags="岗位相关")
@RestController
public class PostController extends BaseController {
    @Autowired
    private PostService service;

    @GetMapping("/post/page")
    @ApiOperation("分页")
    public ResponseModel<PageResultModel<PostEntity>> getPage(PageRequestModel model) {
        return ok(service.getPage(model));
    }

    @GetMapping("/post/tree")
    @ApiOperation("获取岗位树")
    public ResponseModel<PostTreeModel> getTree(){
        return ok(service.getTree());
    }

    @PostMapping("/post/create")
    @ApiOperation("创建")
    public ResponseModel create(@RequestBody PostEntity model) throws RecordExistsException {
        return convertToResponseModel(service.create(model), "创建失败。");
    }

    @PostMapping("/post/update")
    @ApiOperation("修改")
    public ResponseModel update(@RequestBody PostEntity model) throws RecordExistsException {
        return convertToResponseModel(service.update(model), "修改失败。");
    }

    @PostMapping("/post/delete")
    @ApiOperation("删除")
    public ResponseModel delete(@RequestBody List<Integer> ids) {
        return convertToResponseModel(service.removeByIds(ids) ,"删除失败。");
    }
}